<?php
namespace App\Common;

use App\Model\ConfigModel;

/**
 * @name 业务层公用调配
 *
 */

class MyDomain
{
    public $Config = array();//应用配置
    public $ThirdConfig = array();//第三方配置
    public $Lang = array();//消息配置
    public $AppId = 0;

    public function __construct()
    {
        /* $di = \PhalApi\DI();
        $userid = $di->request->get('Uid');
        $joggingLoginCode = $di->request->get('JoggingLoginCode');
        if($userid && $joggingLoginCode){
            return $this->checkUserLoginCode($userid, $joggingLoginCode);
        }else{
            $res['msg'] = '未登入';
            $this->isLogin($res);
        }
        $default = array();
        $this->Config = $di->config->get('app.config', $default);
        $this->ThirdConfig = $di->config->get('sys.third_config', $default);
        $this->Lang = $di->config->get('lang.msgcode', $default); */
    }

    /**
     * 用户登入检查
     * @param integer $userid
     * @param string $joggingLoginCode
     * @date 2018年9月28日 - 下午2:51:29
     * @author JiangYongWei
     */
    protected function checkUserLoginCode($userid, $joggingLoginCode) {
        $where = array('u.userid = ?' => $userid, 'u.flag = ?' => 1, 'u.deleted = ?' => 0);
        $field = 'u.jogginglogincode';
        $userModel = new UserModel();
        $user = $userModel->getUserData($field, $where, 'row');
        if(!$user){
            $this->isLogin(array('code'=>1502, 'msg'=>'用户不存在'));
        }
        if($user['jogginglogincode'] != $joggingLoginCode){
            $this->isLogin(array('code' => 1007, 'msg'=>'登陆信息失效，请重新登陆'));
        }
    }

    /**
     * 登入失败返回
     * @param array $res
     * @date 2018年9月28日 - 下午2:50:45
     * @author JiangYongWei
     */
    protected function isLogin($res = array()) {
        $code = isset($res['code']) ? intval($res['code']) : 0;//信息码
        $msg  = isset($res['msg']) ? $res['msg'] : '';//信息码
        $data = isset($res['data']) ? $res['data'] : array();//信息码
        $di = \PhalApi\DI();
        $di->response->setRet($code);
        $di->response->setMsg($msg);
        $di->response->setData($data);
        $di->response->output();
        die;
    }


    /**
     * 开启事务
     */
    public function transaction()
    {
        \PhalApi\DI()->notorm->beginTransaction('db_master');
    }

    /**
     * 提交事务
     */
    public function commit()
    {
        \PhalApi\DI()->notorm->commit('db_master');
    }

    /**
     * 回滚事务
     */
    public function rollback()
    {
        \PhalApi\DI()->notorm->rollback('db_master');
    }

    /**
     * 获取系统配置
     * @param unknown $varname
     * @return unknown[]
     */
    public function getSysConfig($varname = null)
    {
        $config = new ConfigModel();
        $where = array();
        $where['groupid'] = array(1,2,3,4,7,8,9,10,11,12);
        if($varname){
            if(is_array($varname)){
                $where['varname'] = $varname;
                $res = $config->db_get('varname,value', $where, 'all');
            }else{
                $where['varname = ?'] = $varname;
                $res = $config->db_get('value', $where, 'one');
            }
        }else{
            $res = $config->db_get('varname,value', $where, 'all');
        }
        if(! $res || ! is_array($res)){
            return $res;
        }
        $arr = array();
        foreach($res as $key => $value){
            $arr[$value['varname']] = $value['value'];
        }
        return $arr;
    }

    /**
     * 更新系统配置
     * @param unknown $varname
     * @param unknown $value
     * @return boolean
     */
    public function upSysConfig($varname)
    {
        $sysconfig = new ConfigModel();
        $res = $sysconfig->db_update(array('value' => time()), array('varname = ?' => $varname));
        if(! $res){
            return false;
        }
        return true;
    }
    
}
